<?php
/*
 * Copyright (c) Taras Kudla, 2010
 */
class mod_reviews extends module {

private $table_reviews      ="mod_reviews";
private $messages   = 10;
private $dateformat = "d m, h:i";

public function init(){

    $options=$this->api->options->GetAll();
		
    if(isset($options["messages"])) {
	$this->messages=$options["messages"];
    }

    if(isset($options["dateformat"])) {
	$this->dateformat=$options["dateformat"];
    }


}


public function get(){
    
$total=$this->api->dbo->select($this->table_reviews,"COUNT(*) as count",null,_RESULT_ROW);

$total=$total["count"];

$pages=(int)round($total/$this->messages+0.459);

$page = ( isset($_GET["page"]) && is_numeric($_GET["page"]) && $_GET["page"]<=$pages && $_GET["page"]>0	)?$this->api->escapeString($_GET["page"]):0;

/*
 * Checking for new messages
 */

if($this->api->checkPOST("username","email","message","Submit") ){
    
    list($username,$email,$message) = $this->api->escapePOST("username","email","message");

    if(strlen($username)<3){
        print $this->api->message($this->lang->get("er1"));
    }
    else if(!preg_match("/^[^@]+@[^@]+\.[^@]+$/i",$email,$trash)){
        print $this->api->message($this->lang->get("er2"));
    } else if(strlen($message)<3){
        print $this->api->message($this->lang->get("er3"));
    } else if( $this->api->session->exists("ReviewAdded")  ){
        print $this->api->message($this->lang->get("er4"));
    } else {

        $this->api->dbo->insert($this->table_reviews,array(
            "username"=>$username,
            "email"=>$email,
            "message"=>$message,
            "pubdate"=>date("Y-m-d H:i:s")
        ));

        $this->api->session->ReviewAdded=true;

        if($page!=0){
            $this->api->base->sendHeader("reviews-get","added");
        } else print $this->api->message($this->lang->get("r2"));
    }

}
/*
 * Building list
 */
if(isset($_GET["added"])){
    print $this->api->message($this->lang->get("r2"));
}

$endLimit = $page * $this->messages;

$Query=$this->api->dbo->select($this->table_reviews,"username,pubdate,message"," order by id desc limit ".$this->messages." offset ".$endLimit);

$date=array();

$i=0;while($i<count($Query)){

    if(preg_match("/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})\s*([0-9]{1,2})(:|.)([0-9]{1,2}).*$/s",$Query[$i]["pubdate"],$match)){

            $date[$i]=preg_replace(array("/y/i","/m/i","/d/i","/h/i","/i/i"),array($match[1],$this->month($match[2]),$match[3],$match[4],$match[6]),$this->dateformat);

    } else { $date[$i]="unknown"; }

    $i++;
}

/*
 * Next, previous links generation
 */

$pages--;
$useNext=0;$usePrevious=0;
$Previous=$this->api->base->GetActionLink("reviews-get","page=".($page-1));
$Next=$this->api->base->GetActionLink("reviews-get","page=".($page+1));

if($page>0){

    $usePrevious=1;
    
    if($page==$pages){
        
        $useNext=0;

    } else {
        $useNext=1;
    }

} else if($pages>0) {
    $useNext = 1;
    $usePrevious = 0;
}

/*
 * Building whole table
 */

print $this->api->view->template("list",array("item"=>&$Query,"date"=>&$date,"count"=>count($Query),
    "useNext"=>$useNext,"usePrevious"=>$usePrevious,"next"=>$Next,"previous"=>$Previous) );

}

/*
 * Tools
 */
private function month($m){

$m=(int)$m;

if($m<1 || $m>12) $m=0;

$l=$this->lang->get("m".$m);

return $l;

}


/*
 * Administration
 */

public function admin(){

$total=$this->api->dbo->select($this->table_reviews,"COUNT(*) as count",null,_RESULT_ROW);

$total=$total["count"];

$pages=(int)round($total/$this->messages+0.459);

$page = ( isset($_GET["page"]) && is_numeric($_GET["page"]) && $_GET["page"]<=$pages && $_GET["page"]>0	)?$this->api->escapeString($_GET["page"]):0;

/*
 * Checking for deleting
 */

if(isset($_GET["delete"]) && is_numeric($_GET["delete"]) ){

    $this->api->dbo->delete($this->table_reviews,"where id='".$this->api->escapeString($_GET["delete"])."' ");

}
/*
 * Building list
 */

$endLimit = $page * $this->messages;

$Query=$this->api->dbo->select($this->table_reviews,"username,id,email,pubdate,message"," order by id desc limit ".$this->messages." offset ".$endLimit);

$date=array();

$i=0;while($i<count($Query)){

    if(preg_match("/^([0-9]{4})-([0-9]{1,2})-([0-9]{1,2})\s*([0-9]{1,2})(:|.)([0-9]{1,2}).*$/s",$Query[$i]["pubdate"],$match)){

            $date[$i]=preg_replace(array("/y/i","/m/i","/d/i","/h/i","/i/i"),array($match[1],$this->month($match[2]),$match[3],$match[4],$match[6]),$this->dateformat);

    } else { $date[$i]="unknown"; }

    $i++;
}

/*
 * Next, previous links generation
 */

$pages--;
$useNext=0;$usePrevious=0;
$Previous=$this->api->base->GetActionLink("reviews-admin","page=".($page-1));
$Next=$this->api->base->GetActionLink("reviews-admin","page=".($page+1));

if($page>0){

    $usePrevious=1;

    if($page==$pages){

        $useNext=0;

    } else {
        $useNext=1;
    }

} else if($pages>0) {
    $useNext = 1;
    $usePrevious = 0;
}

/*
 * Building whole table
 */

print $this->api->view->template("listadmin",array("item"=>&$Query,"date"=>&$date,"count"=>count($Query),
    "useNext"=>$useNext,"usePrevious"=>$usePrevious,"next"=>$Next,"previous"=>$Previous,"page"=>$page) );


}


public function options(){

	if($this->api->checkPOST("messages","dateformat")){

	if( is_numeric($_POST["messages"])){

	$this->api->options->Set("reviews","messages",$_POST["messages"]);

	$this->messages = $_POST["messages"];
	}

	$this->api->options->Set("reviews","dateformat",$this->api->escapeString($_POST["dateformat"]));

	$this->dateformat = $_POST["dateformat"];

	}

	return $this->api->view->template("options",array("messages"=>$this->messages,"dateformat"=>$this->dateformat) );
}


/*
 * Install,Unistall
 */

public function install(){

    /*
     * Setting up an options
     */

     $this->api->options->Set("reviews","messages","10");

     $this->api->options->Set("reviews","dateformat","d m, h:i");

    /*
     * Creating table
     */
    
    $table = new sqltable($this->table_reviews);
    $table->addfield("id","int_auto");
    $table->addfield("email","char"); 
    $table->addfield("username","char"); 
    $table->addfield("message","char"); 
    $table->addfield("pubdate","datetime");
    $table->addkey("primary","id");
    $this->api->dbo->createtable($table);
    
}

public function uninstall(){    $this->api->options->Delete("reviews");
    $this->api->dbo->droptable($this->table_reviews);
}


}
?>
